<template>
<div class="login">
<el-form ref="form" :model="form" class="container" :rules="rules">
    <el-form-item >
        <div class="avatar">
            <img src="../assets/avatar.jpg" alt="">
        </div>
    </el-form-item>
  <el-form-item prop="username">
    <el-input v-model="form.username" placeholder="用户名" prefix-icon="myicon myicon-user"></el-input>
  </el-form-item>
   <el-form-item prop="password">
    <el-input v-model="form.password" placeholder="密码" prefix-icon="myicon myicon-key" type="password"></el-input>
  </el-form-item>
  <el-form-item>
       <el-button type="primary"  class="btn_login" @click="LoginBtn('form')">登录</el-button>
  </el-form-item>
</el-form>
</div>
</template>
<script>
import { checkUser } from "@/api/index";
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 6,
            message: "用户名的长度在 3 到 6 个字符",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 8,
            message: "用户名的长度在 6 到 8个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    LoginBtn(form) {
      /* validate()证实; 使合法化，使有法律效力; 使生效; 批准，确认 */
      // 是字符串不可以用点
      this.$refs[form].validate(valid => {
        if (valid) {
          checkUser(this.form).then(res => {
            // console.log(this.form,123);
            if (res.meta.status == 200) {
              // console.log("登录成功");
              // console.log(res.data.username)
              localStorage.setItem('mytoken',res.data.token);
              localStorage.setItem('username',res.data.username)
              this.$router.push({name:'Home'})
            } else {
              this.$message({
                message: res.meta.msg,
                type: "error"
              });
            }
          });
        } else {
          console.log(1343);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  position: fixed;
  background: #2f4050;
  .container {
    width: 400px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    padding: 0px 40px 15px 40px;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .btn_login {
      width: 100%;
    }
  }
}
</style>